home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / FIXNUM.C < prev    next >
C/C++ Source or Header  |  1992-01-14  |  9KB  |  332 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/src/microcode/RCS/fixnum.c,v 9.35 1992/01/15 02:23:28 jinx Exp $
  4.  
  5. Copyright (c) 1987-92 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* Support for fixed point arithmetic.  This should be used instead of
  36.    generic arithmetic when it is desired to tell the compiler to perform
  37.    open coding of fixnum arithmetic.  It is probably a short-term kludge
  38.    that will eventually go away. */
  39.  
  40. #include "scheme.h"
  41. #include "prims.h"
  42.  
  43. static long
  44. DEFUN (arg_fixnum, (n), int n)
  45. {
  46.   fast SCHEME_OBJECT argument = (ARG_REF (n));
  47.   if (! (FIXNUM_P (argument)))
  48.     error_wrong_type_arg (n);
  49.   return (FIXNUM_TO_LONG (argument));
  50. }
  51.  
  52. static long
  53. DEFUN (arg_unsigned_fixnum, (n), int n)
  54. {
  55.   fast SCHEME_OBJECT argument = (ARG_REF (n));
  56.   if (! (FIXNUM_P (argument)))
  57.     error_wrong_type_arg (n);
  58.   return (UNSIGNED_FIXNUM_TO_LONG (argument));
  59. }
  60.  
  61. /* Predicates */
  62.  
  63. DEFINE_PRIMITIVE ("ZERO-FIXNUM?", Prim_zero_fixnum, 1, 1, 0)
  64. {
  65.   PRIMITIVE_HEADER (1);
  66.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((arg_fixnum (1)) == 0));
  67. }
  68.  
  69. DEFINE_PRIMITIVE ("NEGATIVE-FIXNUM?", Prim_negative_fixnum, 1, 1, 0)
  70. {
  71.   PRIMITIVE_HEADER (1);
  72.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((arg_fixnum (1)) < 0));
  73. }
  74.  
  75. DEFINE_PRIMITIVE ("POSITIVE-FIXNUM?", Prim_positive_fixnum, 1, 1, 0)
  76. {
  77.   PRIMITIVE_HEADER (1);
  78.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((arg_fixnum (1)) > 0));
  79. }
  80.  
  81. DEFINE_PRIMITIVE ("EQUAL-FIXNUM?", Prim_equal_fixnum, 2, 2, 0)
  82. {
  83.   PRIMITIVE_HEADER (2);
  84.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((arg_fixnum (1)) == (arg_fixnum (2))));
  85. }
  86.  
  87. DEFINE_PRIMITIVE ("LESS-THAN-FIXNUM?", Prim_less_fixnum, 2, 2, 0)
  88. {
  89.   PRIMITIVE_HEADER (2);
  90.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((arg_fixnum (1)) < (arg_fixnum (2))));
  91. }
  92.  
  93. DEFINE_PRIMITIVE ("GREATER-THAN-FIXNUM?", Prim_greater_fixnum, 2, 2, 0)
  94. {
  95.   PRIMITIVE_HEADER (2);
  96.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((arg_fixnum (1)) > (arg_fixnum (2))));
  97. }
  98.  
  99. /* Operators */
  100.  
  101. #define FIXNUM_RESULT(fixnum)                        \
  102. {                                    \
  103.   fast long result = (fixnum);                        \
  104.   if (! (LONG_TO_FIXNUM_P (result)))                    \
  105.     error_bad_range_arg (1);                        \
  106.   PRIMITIVE_RETURN (LONG_TO_FIXNUM (result));                \
  107. }
  108.  
  109. DEFINE_PRIMITIVE ("ONE-PLUS-FIXNUM", Prim_one_plus_fixnum, 1, 1, 0)
  110. {
  111.   PRIMITIVE_HEADER (1);
  112.   FIXNUM_RESULT ((arg_fixnum (1)) + 1);
  113. }
  114.  
  115. DEFINE_PRIMITIVE ("MINUS-ONE-PLUS-FIXNUM", Prim_m_1_plus_fixnum, 1, 1, 0)
  116. {
  117.   PRIMITIVE_HEADER (1);
  118.   FIXNUM_RESULT ((arg_fixnum (1)) - 1);
  119. }
  120.  
  121. DEFINE_PRIMITIVE ("PLUS-FIXNUM", Prim_plus_fixnum, 2, 2, 0)
  122. {
  123.   PRIMITIVE_HEADER (2);
  124.   FIXNUM_RESULT ((arg_fixnum (1)) + (arg_fixnum (2)));
  125. }
  126.  
  127. DEFINE_PRIMITIVE ("MINUS-FIXNUM", Prim_minus_fixnum, 2, 2, 0)
  128. {
  129.   PRIMITIVE_HEADER (2);
  130.   FIXNUM_RESULT ((arg_fixnum (1)) - (arg_fixnum (2)));
  131. }
  132.  
  133. DEFINE_PRIMITIVE ("FIXNUM-NEGATE", Prim_fixnum_negate, 1, 1, 0)
  134. {
  135.   PRIMITIVE_HEADER (1);
  136.   FIXNUM_RESULT (- (arg_fixnum (1)));
  137. }
  138.  
  139. /* Fixnum multiplication routine with overflow detection. */
  140. #include "mul.c"
  141.  
  142. DEFINE_PRIMITIVE ("MULTIPLY-FIXNUM", Prim_multiply_fixnum, 2, 2, 0)
  143. {
  144.   PRIMITIVE_HEADER (2);
  145.   CHECK_ARG (1, FIXNUM_P);
  146.   CHECK_ARG (2, FIXNUM_P);
  147.   {
  148.     fast long result = (Mul ((ARG_REF (1)), (ARG_REF (2))));
  149.     if (result == SHARP_F)
  150.       error_bad_range_arg (1);
  151.     PRIMITIVE_RETURN (result);
  152.   }
  153. }
  154.  
  155. DEFINE_PRIMITIVE ("DIVIDE-FIXNUM", Prim_divide_fixnum, 2, 2, 0)
  156. {
  157.   fast long numerator;
  158.   fast long denominator;
  159.   fast long quotient;
  160.   fast long remainder;
  161.   PRIMITIVE_HEADER (2);
  162.   numerator = (arg_fixnum (1));
  163.   denominator = (arg_fixnum (2));
  164.   if (denominator == 0)
  165.     error_bad_range_arg (2);
  166.   /* Now, unbelievable hair because C doesn't fully specify / and %
  167.      when their arguments are negative.  We must get consistent
  168.      answers for all valid arguments. */
  169.   if (numerator < 0)
  170.     {
  171.       numerator = (- numerator);
  172.       if (denominator < 0)
  173.     {
  174.       denominator = (- denominator);
  175.       quotient = (numerator / denominator);
  176.     }
  177.       else
  178.     quotient = (- (numerator / denominator));
  179.       remainder = (- (numerator % denominator));
  180.     }
  181.   else
  182.     {
  183.       if (denominator < 0)
  184.     {
  185.       denominator = (- denominator);
  186.       quotient = (- (numerator / denominator));
  187.     }
  188.       else
  189.     quotient = (numerator / denominator);
  190.       remainder = (numerator % denominator);
  191.     }
  192.   if (! (LONG_TO_FIXNUM_P (quotient)))
  193.     error_bad_range_arg (1);
  194.   PRIMITIVE_RETURN
  195.     (cons ((LONG_TO_FIXNUM (quotient)), (LONG_TO_FIXNUM (remainder))));
  196. }
  197.  
  198. DEFINE_PRIMITIVE ("FIXNUM-QUOTIENT", Prim_fixnum_quotient, 2, 2, 0)
  199. {
  200.   PRIMITIVE_HEADER (2);
  201.   {
  202.     fast long numerator = (arg_fixnum (1));
  203.     fast long denominator = (arg_fixnum (2));
  204.     fast long quotient =
  205.       ((denominator > 0)
  206.        ? ((numerator < 0)
  207.       ? (- ((- numerator) / denominator))
  208.       : (numerator / denominator))
  209.        : (denominator < 0)
  210.        ? ((numerator < 0)
  211.       ? ((- numerator) / (- denominator))
  212.       : (- (numerator / (- denominator))))
  213.        : (error_bad_range_arg (2), 0));
  214.     if (! (LONG_TO_FIXNUM_P (quotient)))
  215.       error_bad_range_arg (1);
  216.     PRIMITIVE_RETURN (LONG_TO_FIXNUM (quotient));
  217.   }
  218. }
  219.  
  220. DEFINE_PRIMITIVE ("FIXNUM-REMAINDER", Prim_fixnum_remainder, 2, 2, 0)
  221. {
  222.   PRIMITIVE_HEADER (2);
  223.   {
  224.     fast long numerator = (arg_fixnum (1));
  225.     fast long denominator = (arg_fixnum (2));
  226.     PRIMITIVE_RETURN
  227.       (LONG_TO_FIXNUM
  228.        ((denominator > 0)
  229.     ? ((numerator < 0)
  230.        ? (- ((- numerator) % denominator))
  231.        : (numerator % denominator))
  232.     : (denominator < 0)
  233.     ? ((numerator < 0)
  234.        ? (- ((- numerator) % (- denominator)))
  235.        : (numerator % (- denominator)))
  236.     : (error_bad_range_arg (2), 0)));
  237.   }
  238. }
  239.  
  240. DEFINE_PRIMITIVE ("GCD-FIXNUM", Prim_gcd_fixnum, 2, 2, 0)
  241. {
  242.   fast long x;
  243.   fast long y;
  244.   fast long z;
  245.   PRIMITIVE_HEADER (2);
  246.   x = (arg_fixnum (1));
  247.   y = (arg_fixnum (2));
  248.   if (x < 0) x = (-x);
  249.   if (y < 0) y = (-y);
  250.   while (y != 0)
  251.     {
  252.       z = x;
  253.       x = y;
  254.       y = (z % y);
  255.     }
  256.   PRIMITIVE_RETURN (LONG_TO_FIXNUM (x));
  257. }
  258.  
  259. /* Bitwise operations */
  260.  
  261. #define FIXNUM_BOOLEAN_BODY(operation)                    \
  262. do                                    \
  263. {                                    \
  264.   fast unsigned long x, y, z;                        \
  265.                                     \
  266.   PRIMITIVE_HEADER (2);                            \
  267.                                     \
  268.   x = (arg_unsigned_fixnum (1));                    \
  269.   y = (arg_unsigned_fixnum (2));                    \
  270.                                     \
  271.   z = (x operation y);                            \
  272.   return (LONG_TO_FIXNUM (z));                        \
  273. } while (0)
  274.  
  275.  
  276. DEFINE_PRIMITIVE ("FIXNUM-ANDC", Prim_fixnum_andc, 2, 2, 0)
  277. {
  278.   FIXNUM_BOOLEAN_BODY(& ~);
  279. }
  280.  
  281.  
  282. DEFINE_PRIMITIVE ("FIXNUM-AND", Prim_fixnum_and, 2, 2, 0)
  283. {
  284.   FIXNUM_BOOLEAN_BODY(&);
  285. }
  286.  
  287.  
  288. DEFINE_PRIMITIVE ("FIXNUM-OR", Prim_fixnum_or, 2, 2, 0)
  289. {
  290.   FIXNUM_BOOLEAN_BODY(|);
  291. }
  292.  
  293.  
  294. DEFINE_PRIMITIVE ("FIXNUM-XOR", Prim_fixnum_xor, 2, 2, 0)
  295. {
  296.   FIXNUM_BOOLEAN_BODY(^);
  297. }
  298.  
  299.  
  300. DEFINE_PRIMITIVE ("FIXNUM-NOT", Prim_fixnum_not, 1, 1, 0)
  301. {
  302.   fast unsigned long x, z;
  303.  
  304.   PRIMITIVE_HEADER (1);
  305.  
  306.   x = (arg_unsigned_fixnum (1));
  307.  
  308.   z = (~ (x));
  309.   return (LONG_TO_FIXNUM (z));
  310. }
  311.  
  312. DEFINE_PRIMITIVE ("FIXNUM-LSH", Prim_fixnum_lsh, 2, 2, 0)
  313. {
  314.   fast unsigned long x, z;
  315.   fast long y;
  316.  
  317.   PRIMITIVE_HEADER (2);
  318.  
  319.   x = (arg_unsigned_fixnum (1));
  320.   y = (arg_fixnum (2));
  321.  
  322.   if (y < 0)
  323.   {
  324.     z = ((y < (- FIXNUM_LENGTH)) ? 0 : (x >> (- y)));
  325.   }
  326.   else
  327.   {
  328.     z = ((y > FIXNUM_LENGTH) ? 0 : (x << y));
  329.   }
  330.   return (LONG_TO_FIXNUM (z));
  331. }
  332.